﻿using DLUProject.Domain;
using DLUProject.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ColorLife.Core.Helper;

public partial class HeThong_AccountGroup_Permission : BaseUserControl
{
    public IServices<AccountGroup> _service { get; set; }
    public IServices<WorkGroup> _workGroupService { get; set; }
    public IServices<Function> _functionService { get; set; }
    public AccountGroupExtension _accountGroupEx { get; set; }
    public AccountGroup AccountGroup;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        AccountGroup = _service.Get(ID);
        if (AccountGroup == null)
        {
            Response.Redirect("Default.aspx");
        }
        if (!IsPostBack)
        {
            BinMenuToTreeView();
        }
        
    }
    public int ID
    {
        get
        {
            if (Request.QueryString["id"] != null)
                return int.Parse(Server.UrlEncode(Request.QueryString["id"]));
            return -1;
        }
    }
    private List<WorkGroup> GetBySystemID(int sysId)
    {
        return _workGroupService.Table.Where(c => c.SystemID == sysId).ToList();
    }
    private List<Function> GetByWorkGroupID(int sysId)
    {
        return _functionService.Table.Where(c => c.WorkGroupID == sysId).ToList();
    }
    public void BinMenuToTreeView()
    {
        TreeMenu.Nodes.Clear();
        List<WorkGroup> list = GetBySystemID(Session["SystemID"].ToInt());
        foreach (var item  in list)
        {
            TreeNode node = new TreeNode();
            node.Text = item.Name;
            node.Value = item.WorkGroupID.ToString() ;
            node.SelectAction = TreeNodeSelectAction.Expand;
            TreeMenu.Nodes.Add(node);
            List<Function> list1 = GetByWorkGroupID(item.WorkGroupID);
            foreach (var  i in list1)
            {
                TreeNode node1 = new TreeNode();
                node1.Text = i.Name;
                node1.Value = i.FunctionID.ToString();
                node.ChildNodes.Add(node1);
            }
        }
    }
    protected void btnSave_ServerClick(object sender, EventArgs e)
    {
        
        string s = "";
        TreeNodeCollection nodes = this.TreeMenu.Nodes;
        foreach (TreeNode n in nodes)
        {
            if (n.Checked)
            {
                // s += n.Value;

                foreach (TreeNode tn in n.ChildNodes)
                {
                    _accountGroupEx.AddFunctionToGroup(ID, tn.Value.ToInt());
                }
            }
        }
        
       // Label1.Text=s;
    }
    //private void GetNodeRecursive(TreeNode treeNode)
    //{
    //    if (treeNode.Checked == true)
    //    {

    //        //Your Code Goes Here to perform any action on checked node
    //    }
    //    foreach (TreeNode tn in treeNode.ChildNodes)
    //    {
    //        GetNodeRecursive(tn);
    //    }

    //}
}